﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_NoSAPFin_NoSAPFin001b : AdminBasePage
{
    decimal? receiptId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ReceiptID"] != null)
        {
            receiptId = decimal.Parse(Request.QueryString["ReceiptID"]);
        }
        if (!IsPostBack)
        {
            if (receiptId.HasValue)
            {
                FinReceipt finReceipt = new FinReceipt();
                if (finReceipt.LoadByPrimaryKey(receiptId.Value))
                {
                    if (finReceipt.StorageLocation.HasValue)
                    {
                        drpThirdUnit.SelectedValue = finReceipt.StorageLocation.Value.ToString();
                        BFThirdUnitCabinCollection bFThirdUnitCabinCollection = new BFThirdUnitCabinCollection();
                        bFThirdUnitCabinCollection.Query.Where(bFThirdUnitCabinCollection.Query.ThirdUnitID == finReceipt.StorageLocation)
                            .OrderBy(bFThirdUnitCabinCollection.Query.Name.Ascending);
                        bFThirdUnitCabinCollection.Query.Load();
                        drpCabin.DataSource = bFThirdUnitCabinCollection;
                        drpCabin.DataTextField = BFThirdUnitCabinMetadata.ColumnNames.Name;
                        drpCabin.DataValueField = BFThirdUnitCabinMetadata.ColumnNames.ThirdUnitCabinID;
                        drpCabin.DataBind();

                        drpCabin.Items.Insert(0, new ListItem("请选择仓库号", ""));
                    }
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (receiptId.HasValue)
        {
            FinReceipt finReceipt = new FinReceipt();
            FinBox finBox = new FinBox();
            string user = LoginUserInfo.CurrentUser.LoginName;
            DateTime today = DateTime.Now;


            using (esTransactionScope tr = new esTransactionScope())
            {
                if (finReceipt.LoadByPrimaryKey(receiptId.Value))
                {
                    finReceipt.ThirdUnitCabinId = Guid.Parse(drpCabin.SelectedValue);
                    finReceipt.ThirdUnitNumberId = Guid.Parse(drpNumber.SelectedValue);
                    finReceipt.FirstStorageDate = today;
                    finReceipt.StoreStartDate = DateTime.Parse(today.Year.ToString() + "-12-31");
                    finReceipt.StorageStatus = "S2";
                    finReceipt.UpdatedBy = user;
                    finReceipt.UpdatedDttm = today;
                    finReceipt.VersionNo = finReceipt.VersionNo + 1;
                    finReceipt.Save();
                    if (finReceipt.BoxID.HasValue)
                    {
                        if (finBox.LoadByPrimaryKey(finReceipt.BoxID.Value))
                        {
                            finBox.ThirdUnitCabinId = Guid.Parse(drpCabin.SelectedValue);
                            finBox.ThirdUnitNumberId = Guid.Parse(drpNumber.SelectedValue);
                            finBox.UpdatedBy = user;
                            finBox.UpdatedDttm = today;
                            finBox.VersionNo = finBox.VersionNo + 1;
                            finBox.StorageStatus = "S2";
                            finBox.Save();
                        }
                    }
                }
                

                tr.Complete();


            }

        }

        WebHelper.CloseDialog(true);



    }

    protected void drpCabin_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(drpCabin.SelectedValue))
        {
            BFThirdUnitNumberCollection bFThirdUnitNumberCollection = new BFThirdUnitNumberCollection();
            bFThirdUnitNumberCollection.Query.Where(bFThirdUnitNumberCollection.Query.ThirdUnitCabinID == Guid.Parse(drpCabin.SelectedValue))
                .OrderBy(bFThirdUnitNumberCollection.Query.Name.Ascending);
            bFThirdUnitNumberCollection.Query.Load();
            drpNumber.DataSource = bFThirdUnitNumberCollection;
            drpNumber.DataTextField = BFThirdUnitNumberMetadata.ColumnNames.Name;
            drpNumber.DataValueField = BFThirdUnitNumberMetadata.ColumnNames.ThirdUnitNumberID;
            drpNumber.DataBind();

            drpNumber.Items.Insert(0, new ListItem("请选择凭证柜号", ""));
        }
    }
}